package com.jeff.grailsstudy.service

import com.jeff.grailsstudy.Book
import com.jeff.grailsstudy.Author
import com.jeff.grailsstudy.embeded.Country
import com.jeff.grailsstudy.Airport
class DataService {

    def prepareInitData() {
        for (i in 0..6) {
            if (i < 4) {
                def airport = new Airport(name: "Asia_$i", slot: i + 8, termDate: new Date())
                airport.save(flush: true)
            } else {
                def airport = new Airport(name: "Africa_$i", slot: i + 8, termDate: new Date())
                airport.save(flush: true)
            }
            
        }
        
        def author1 = new Author(name: "Jeff", nickName: "Jeff the Baptist", age: 32, 
                country: new Country(cname: "China", ciso3: "CHN"))
//        author1.name = "Jeff"
//        author1.nickName = "Jeff the Baptist"
//        author1.age = 32
        author1.save(flush: true)
        
        def books = []
        for (i in 0..2) {
            def book = new Book(title: "MyBook_$i", price: 3.25 + i, 
                                publisher: "MyPublisher", released: new Date(), 
                                author: author1)
            
            if (!book.save(flush: true)) {
                //if set grails.gorm.failOnError property no need below code
                book.errors.each {
                    print it
                }
            }
            books.add(book)
        }
        
        author1.books = books //Why?! If leave without this, later when author 
                              //searched out, author.books no value at all. 
                              //book.author=author1 not enough for the 1-to-many?
        
        def author2 = new Author(name: "Winnie Wang", 
                country: new Country(cname: "China", ciso3: "CHN"))
        //simulate web request params
        def params = [name: "Winnie Wang", nickName: "Winnie Baby", age: 30]
        author2.properties = params //binding to params
        author2.save(flush: true)
    }
    
    def tryInitData() {
        def author = Author.findByName("Jeff")
//        def author = Author.findByCountry(new Country(cname: "China", ciso3: "CHN"))
        author.books.each {
            print it.title
        }
        
        def author2 = Author.findByName("Winnie Wang")
        
        print "Books between \$4 & \$6: " + Book.countByPriceBetween(4, 6)
        
        Book.findAll().each { book ->
            if (book.price < 4 && book.price >= 3) {
                book.title = "${book.price}_titled"
            } else if (book.price < 5 && book.price >= 4) {
                book.price = 6.0
                book.discard() //why doesn't work? below line still got persisted, 
                                //seems like if I leave without "author1.books = books", 
                                //discard() here works!? why?!
                book.title = "${book.price}_titled"
            }
            
            println "** this book identity is: ${book.ident()}"
        }
        
        def dupBook = new Book(id: 1, title: "New Book Name", price: 7.0, 
                                publisher: "NoPublisher", released: new Date(), 
                                author: author2)
        println "** Before merge(), dupBook is detached: ${!dupBook.attached}"
        
        dupBook = dupBook.merge()
        println "** After merge(), dupBook is detached: ${!dupBook.attached}"
        
        def asiaAirportsWith10Slots = Airport.asianAirports.haveTenSlots.list()
        println asiaAirportsWith10Slots.each {}
        
        def asiaAirportsWithSpecifiedSlots = Airport.asianAirports.findAllBySlot(11)
        println asiaAirportsWithSpecifiedSlots.each {}
        
        def asiaAirportsWith8n9slots = Airport.asianAirports {
            or {
                eq 'slot', 8
                eq 'slot', 9
            }
        }
        println asiaAirportsWith8n9slots.each {}
        
        def authorHaveMyBook = Author.withBooksTitledMyBook.get()
        println authorHaveMyBook
    }
}
